package com.ekingstar.hedss.action;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.service.UserExistException;
import com.ekingstar.hedss.domain.HedssZllb;
import com.ekingstar.hedss.domain.HedssZlxx;
import com.ekingstar.hedss.service.IHedssService;

@SuppressWarnings({"serial","unchecked"})
@Action(name="addOrUpdateZlxx",namespace="/documentation")
@Results({
	@Result(name = "input", value = "/pages/documentation/zlxx-input.jsp"),	 
	@Result(name="list",value="documentation!listZlxxPage",type=ServletActionRedirectResult.class,params={"namespace","/documentation","searchzlxx.TZllb.id","${searchzlxx.TZllb.id}"})
		})
public class AddOrUpdateZlxxAction extends ActionSupport implements Preparable {
	private IHedssService hedssService;
	
	private List<HedssZllb> zllbList;	

	private HedssZlxx searchzlxx;
	private HedssZlxx zlxx;
	
	public String input(){
		if (zlxx != null && zlxx.getId() !=null){
			zlxx = hedssService.viewZlxx(zlxx.getId());
		}
		else{
			if(searchzlxx!=null&&searchzlxx.getTZllb()!=null&&searchzlxx.getTZllb().getId()!=null){
				zlxx=new HedssZlxx();
				HedssZllb zllb=hedssService.getZllb(searchzlxx.getTZllb().getId());
				zlxx.setTZllb(zllb);
			}
		}
		
		return "input";
	}
	
	@Validations(
			requiredStrings={
					@RequiredStringValidator(message="资料标题必填",fieldName="zlxx.title")},
					
			regexFields={
					}
			)
	public String execute(){
		
		boolean logged = UserUtil.isLogged();
		if (!logged ){
			return "nologin";
		}
		User user= UserUtil.getCurrentUser();
		
		try{			
			if(zlxx.getMemo()!=null&&zlxx.getMemo().length()>150){
				this.addFieldError("zlxx.memo","备注过长。");
				return "input";
			}
			if (zlxx != null && zlxx.getId() != null && !zlxx.getId().trim().equals("")){
				zlxx.setLastUpdateDate(new Date());
				zlxx.setUpdaterId(user.getName());
				hedssService.updateZlxx(zlxx);
			}else {
				zlxx.setAddDate(new Date());
				zlxx.setAdderId(user.getName());
				hedssService.addZlxx(zlxx);
			}
		}catch(UserExistException e){
			this.addFieldError(ERROR,e.getMessage());
			return "input";
		}
		
		return "list";
	}
	
	public IHedssService getHedssService() {
		return hedssService;
	}

	public void setHedssService(IHedssService hedssService) {
		this.hedssService = hedssService;
	}

	public HedssZlxx getZlxx() {
		return zlxx;
	}

	public void setZlxx(HedssZlxx zlxx) {
		this.zlxx = zlxx;
	}

	public HedssZlxx getSearchzlxx() {
		return searchzlxx;
	}

	public void setSearchzlxx(HedssZlxx searchzlxx) {
		this.searchzlxx = searchzlxx;
	}

	public void prepare() throws Exception {
		Map tmp = new HashMap();
		tmp.put("", "----请选择----");
		if (zllbList == null) {
			HedssZllb searchzllb=new HedssZllb();
			zllbList =(hedssService.getZllblist(searchzllb, 0, -1)).getRes();
			
		}
		
	}

	public List<HedssZllb> getZllbList() {
		return zllbList;
	}

	public void setZllbList(List<HedssZllb> zllbList) {
		this.zllbList = zllbList;
	}
		
}
